<?php

namespace App\Http\Controllers\Admin;

use App\Models\Activity;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\LayuiController as Layui;
use App\Exceptions\FormException;
use Illuminate\Support\Facades\View;
use Mockery\Exception;

class ActivityController extends Controller
{
    /**
     * 列表页面
     *
     * @Author   stonefall
     * @DateTime 2019-06-5
     * @return   [type]     [description]
     */
    public function index()
    {
        return view('Admin.activity.index');
    }

    /**
     * 获取数据
     * @return [json]           [数组格式]
     */
    public function table(Request $request){
        //关联模型查询
        $activityData = Activity::orderBy('id', 'DESC')
            ->paginate($request->get('limit',10));
        return Layui::tableJson($activityData,0,1);
    }



    /**
     * 添加页面
     *
     * @Author   stonefall
     * @DateTime 2019-06-5
     * @param    Request    $request [description]
     * @return   [type]              [description]
     */
    public function create()
    {
        if (View::exists('Admin/activity/add')) {
            return view('Admin/activity/add');
        } else {
            return 'View Not Found';
        }
    }

    /**
     * 添加请求
     *
     * @Author   stonefall
     * @DateTime 2019-06-5
     * @param    Request    $request [description]
     * @return   [type]              [description]
     */
    public function store(Request $request)
    {
        $payload = $this->packageData($request);
        try{
            Activity::create($payload);
        }catch (\Exception $exception){
            throw new FormException(\App::environment('local') ? $exception->getMessage() : '保存失败' );
        }
        return $this->getResponseSuccess('保存成功',route('activity.index'));
    }



    /**
     * 编辑页面
     *
     * @Author   stonefall
     * @DateTime 2019-05-31
     * @param    Request $request [description]
     * @return   [type]                [description]
     */
    public function edit($id)
    {
        if (View::exists('Admin/activity/edit')) {
            $data = Activity::find($id);
            return view('Admin/activity/edit',compact('data'));
        } else {
            return 'View Not Found';
        }
    }

    /**
     * 修改请求
     *
     * @Author   stonefall
     * @DateTime 2019-05-31
     * @param    Request $request [description]
     * @return   [type]                [description]
     */
    public function update(Request $request,$id)
    {
        $payload = $this->packageData($request);
        try{
            if ($id){
                Activity::where(['id'=>$id])->update($payload);
            }else{
                throw new Exception('参数错误');
            }
        }catch (\Exception $exception){
            throw new FormException(\App::environment('local') ? $exception->getMessage() : '保存失败' );
        }
        return $this->getResponseSuccess('保存成功',route('activity.index'));
    }

    /**
     * 修改状态请求
     *
     * @Author   stonefall
     * @DateTime 2019-05-31
     * @param    Request $request [description]
     * @return   [type]                [description]
     */
    public function upstatus(Request $request,$id)
    {
        try{ 
            $activity = Activity::findOrFail($id);
            $activity->status = $activity->status == 1 ? 2 : 1;
            $activity->save(); 
        }catch (\Exception $exception){
            throw new FormException(\App::environment('local') ? $exception->getMessage() : '保存失败' );
        }
        return $this->getResponse('操作成功');
    }


    /**
     * 拼装共用请求的数据
     *
     * @Author   stonefall
     * @DateTime 2019-05-31
     * @param    [type]     $request [description]
     * @return   [type]              [description]
     */
    public function packageData($request)
    {
        $payloads = $request->all(['activity_name','jump_url','status']);
        $payload['activity_name'] = $payloads['activity_name'];
        $payload['jump_url'] = isset($payloads['jump_url']) ? $payloads['jump_url'] : '';
        $payload['status'] = isset($payloads['status']) ? $payloads['status'] : 2;
        return $payload;
    }


    /**
     * 删除
     *
     * @Author   stonefall
     * @DateTime 2019-06-5
     * @param    [type]     $request [description]
     * @param    [type]     $id [description]
     * @return   [type]         [description]
     */
    public function destroy(Request $request,$id)
    {
        \DB::beginTransaction();
        try{
            Activity::destroy($id);
        }catch (\Exception $exception){
            \DB::rollBack();
            throw new FormException(App::environment('local') ? $exception->getMessage() : '操作失败' );
        }
        \DB::commit();
        return $this->getResponse('操作成功');
    }
}
